package one.stand.factory;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import one.stand.entity.TokenEntity;
import one.stand.entity.websocket.*;
import one.stand.service.UserChannelService;
import one.stand.wesocket.code.CommandCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author Administrator
 */
@Component
public class MessageFactoryEntrance {
    private Logger logger = LoggerFactory.getLogger(MessageFactoryEntrance.class);
    @Autowired
    private UserChannelService userChannelService;
    @Async("socketTask")
    public void entrance(ChannelHandlerContext ctx, String text) {
        if (StringUtils.isEmpty(text)) {
            logger.info("收到数据为空的消息，不进行处理");
            return;
        }


        JSONObject jsonObject = JSONObject.parseObject(text);
        int code = jsonObject.getInteger("code");
        //进行token验证
        BaseSocketEntity entity = jsonObject.toJavaObject(BaseSocketEntity.class);
        TokenEntity tokenEntity = userChannelService.channelV(ctx, entity);
        if (null == tokenEntity) {
            return;
        }
        entity.setId(tokenEntity.getId());
        entity.setCompanyId(tokenEntity.getCId());
        entity.setLoginProgramName(tokenEntity.getLoginProgramName());
        jsonObject.put("id", tokenEntity.getId());
        jsonObject.put("companyId", tokenEntity.getCId());
        jsonObject.put("loginProgramName", tokenEntity.getLoginProgramName());
        jsonObject.put("time", System.currentTimeMillis());
        //测试代码
        //if(jsonObject.containsKey("targetId") && jsonObject.getString("targetId").equals("undefined")){
            //jsonObject.put("targetId",6915);
        //}
        switch (code) {
            case CommandCode.SOCKET_REGISTER:
                //注册流程
                userChannelService.register(ctx, entity);
                break;
            case CommandCode.SOCKET_HEARTBEAT:
                //心跳流程
                userChannelService.heartbeat(ctx, entity);
                break;
            case CommandCode.SOCKET_SENDMESSAGE:
                //发送消息
                SendMessageEntity sendMessageEntity = jsonObject.toJavaObject(SendMessageEntity.class);
                userChannelService.sendMessage(ctx, sendMessageEntity);
                break;
            case CommandCode.SOCKET_SEE:
                //读取消息
                userChannelService.seeMessage(ctx, jsonObject.toJavaObject(SeeMessageEntity.class));
                break;
            case CommandCode.SOCKET_GET_MESSAGE:

                //获取消息
                GetMessageEntity getMessageEntity = jsonObject.toJavaObject(GetMessageEntity.class);
                userChannelService.getMessage(ctx, getMessageEntity);
                break;
            case CommandCode.SOCKET_DYNAMIC_FABULOUS:
                //动态点赞
                DynamicFabulousEntity dynamicFabulousEntity = jsonObject.toJavaObject(DynamicFabulousEntity.class);
                userChannelService.dynamicFabulous(ctx, dynamicFabulousEntity);
                break;
            case CommandCode.SOCKET_DYNAMIC_REVIEW:
                //动态评论
                DynamicReviewEntity dynamicReviewEntity = jsonObject.toJavaObject(DynamicReviewEntity.class);
                userChannelService.dynamicReview(ctx, dynamicReviewEntity);
                break;
            case CommandCode.SOCKET_IMLIST:
                //聊天列表 crm和名片获取列表都使用此方法
                GetImListEntity imListEntity = jsonObject.toJavaObject(GetImListEntity.class);
                userChannelService.getImList(ctx, imListEntity);
                break;
            case CommandCode.SOCKET_UNREAD_MESSAGE_NUM:
                //获取和用户的未读消息数量
                userChannelService.getUnreadMessageNum(ctx, jsonObject.toJavaObject(GetUnreadMessageNumEntity.class));
                break;
            default:
                logger.info("错误命令码：" + text);
                ctx.close();
                break;
        }
    }

}
